# Reindeer OS - GNU make build script
#
# Copyrights 2011 Michal Saman, m.saman@designplus.cz.
# This source code is release under the Apache License 2.0.
# http://www.apache.org/licenses/LICENSE-2.0

# TARGET architecture
ARCH = ia32

# compilation environment detection
UNAME := $(shell uname -s)

# default linker
LD = ld

# default search path
VPATH = boot:crtlib:hal:kernel

# delete utility
RM	= rm

# output directory
OUT_DIR = debug

# set default C compiler
CC = gcc

# set default assembler compiler
AC = nasm

#INCLUDES = kernel/common/include libc/include kernel/arch/$(ARCH)/include 

# set default flags for compiler
CFLAGS = \
	-m32 \
	-nostdinc \
	-Ikernel/common/include \
	-Ilibc/include \
	-Ikernel/arch/$(ARCH)/include \
	-fno-builtin \
	-fno-exceptions \
	-fno-unwind-tables \
	-fpack-struct=1 \
	-finline-functions \
	-DARCH_X86 \
	-DALLOC_FREE_REORDER \
	-fverbose-asm \
	-dA \
	-fno-stack-protector
	
ACFLAGS = \
	-f elf
		
LDFLAGS = \
	-T link.ld \
	--oformat binary \
	-N \
	-Map $(OUT_DIR)/rkernel_map \
	-m i386linux

ifeq ($(UNAME),Darwin)
LD = /opt/local/bin/i386-mingw32-ld
CC = /opt/local/bin/gcc-mp-4.5
endif
	
# floppy image name (for floppy distribution)
FLOPPY_IMAGE = reindeeros_floppy.img

# iso9660 image name (for cdrom distribution)
ISO9660_IMAGE = reindeeros_iso9660.iso

# remote directory to distribute finally image
REMOTE = /media/samba

# directories of project
DIRS = kernel/common/src/ libc/src/ kernel/arch/*/src/

# kernel sources common for all architectures
COMMON_SOURCES = \
	kernel/common/src/main/entrypoint.c \
	kernel/common/src/main/kprintf.c \
	kernel/common/src/main/main.c \
	kernel/common/src/main/panic.c \
	kernel/common/src/lib/ordered_array.c \
	kernel/common/src/lib/queue.c \
	kernel/common/src/mem/allocator.c \
	kernel/common/src/mem/kheap.c \
	kernel/common/src/mem/pmu.c \
	kernel/common/src/proc/process.c \
	kernel/common/src/tsh/tsh.c \
	libc/src/string.c
	
-include kernel/arch/$(ARCH)/Makefile

# object files
COMMON_OBJECTS := $(patsubst %.c,%.o,$(COMMON_SOURCES))
ARCH_OBJECTS := $(patsubst %.c,%.o,$(ARCH_SOURCES))
ARCH_OBJECTS_ASM := $(patsubst %.asm,%.o,$(ARCH_SOURCES_ASM))

# makro for call commands with nice console output
quiet = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))

# TARGETS definition
all : rbooter-floppy rbooter-iso9660 rkernel.img

run-floppy : install-floppy
	bochs -f bochs_conf -q 'boot:floppy'
	
run-iso9660 : install-iso9660
	bochs -f bochs_conf -q 'boot:cdrom'	

install-floppy : all
ifeq ($(UNAME),Linux)
	if test -f $(FLOPPY_IMAGE); then rm $(FLOPPY_IMAGE); fi
	mkdosfs -C -F 12 $(FLOPPY_IMAGE) 1440
	mount /media/floppy
	cp $(OUT_DIR)/rdkrld.sys /media/floppy
	cp $(OUT_DIR)/rkernel.img /media/floppy
	umount /media/floppy
	dd conv=notrunc if=$(OUT_DIR)/boot_sector_fat12.bin of=$(FLOPPY_IMAGE) bs=1 count=3
	dd conv=notrunc if=$(OUT_DIR)/boot_sector_fat12.bin of=$(FLOPPY_IMAGE) bs=1 count=450 skip=61 seek=61
	if test -d $(REMOTE); then cp $(FLOPPY_IMAGE) $(REMOTE)/reindeer_vm.img; fi
	if test -d $(REMOTE); then cp $(FLOPPY_IMAGE) $(REMOTE)/reindeer_vb.img; fi
endif
ifeq ($(UNAME),Darwin)
	hdiutil create -fs MS-DOS -fsargs "-F 12 -v REINDEER" -sectors 1440 $(FLOPPY_IMAGE)
	hdiutil attach $(FLOPPY_IMAGE).dmg
	cp $(OUT_DIR)/rdkrld.sys /media/floppy
	cp $(OUT_DIR)/rkernel.img /Volumes/REINDEER
	hdiutil detach /Volumes/REINDEER
	dd conv=notrunc if=$(OUT_DIR)/boot_sector_fat12.bin of=$(FLOPPY_IMAGE) bs=1 count=3
	dd conv=notrunc if=$(OUT_DIR)/boot_sector_fat12.bin of=$(FLOPPY_IMAGE) bs=1 count=450 skip=61 seek=61
endif

install-iso9660 : all
	rm -Rf $(OUT_DIR)/iso9660_release/*
	cp $(OUT_DIR)/rdkrld.sys $(OUT_DIR)/iso9660_release
	cp $(OUT_DIR)/rkernel.img $(OUT_DIR)/iso9660_release
	mkdir $(OUT_DIR)/iso9660_release/test_dir
	mkdir $(OUT_DIR)/iso9660_release/test2
	mkdir $(OUT_DIR)/iso9660_release/test3
	#cp /usr/include/*.h $(OUT_DIR)/iso9660_release/
	cp $(OUT_DIR)/boot_sector_iso9660.bin $(OUT_DIR)/iso9660_release/boot.bin
	if test -f $(ISO9660_IMAGE); then rm $(ISO9660_IMAGE); fi
	mkisofs -b boot.bin -no-emul-boot -iso-level 1 -boot-load-size 4 -boot-info-table -o $(ISO9660_IMAGE) $(OUT_DIR)/iso9660_release
	if test -d $(REMOTE); then cp $(ISO9660_IMAGE) $(REMOTE)/reindeer_vm.iso; fi

rkernel.img : $(COMMON_OBJECTS) $(ARCH_OBJECTS) $(ARCH_OBJECTS_ASM)
	$(call quiet,$(LD) $(LDFLAGS) -o $(OUT_DIR)/$@ $^,"  LD   $@")
	
rbooter-floppy : boot_sector_fat12 rdkrld

rbooter-iso9660 : boot_sector_iso9660 rdkrld
	
boot_sector_fat12 : boot_sector_fat12.asm
	$(call quiet,$(AC) -f bin $^ -o $(OUT_DIR)/boot_sector_fat12.bin,"  AS   boot_sector_fat12.bin")
	
boot_sector_iso9660 : boot_sector_iso9660.asm
	$(call quiet,$(AC) -f bin $^ -o $(OUT_DIR)/boot_sector_iso9660.bin,"  AS   boot_sector_iso9660.bin")
	
rdkrld : rdkrld.asm
	$(call quiet,$(AC) -f bin $^ -o $(OUT_DIR)/rdkrld.sys,"  AS   rdkrld.sys")	
	
$(COMMON_OBJECTS) : %.o : %.c
	$(call quiet,$(CC) $(CFLAGS) -c $< -o $@,"  CC   $@")
	
$(ARCH_OBJECTS) : %.o : %.c
	$(call quiet,$(CC) $(CFLAGS) -c $< -o $@,"  CC   $@")			
	
$(ARCH_OBJECTS_ASM) : %.o : %.asm
	$(call quiet,$(AC) $(ACFLAGS) $< -o $@,"  AS   $@")		
	
.PHONY: clean all install-floppy run-floppy

clean: 
	@for dir in $(DIRS); do \
	echo "Clearing in $$dir..."; \
	(find $$dir/ -name '*.o' -follow -exec $(RM) \{\} \;); done
	$(RM) -frv $(FLOPPY_IMAGE)
	$(RM) -frv $(ISO9660_IMAGE)